// 11/04/2009 Dennis
// this class links frontend and backend
class Application
{
  Form userForm;
  Form artistForm;
  Form albumForm;
  Form listForm;
  Form tracksForm;
  Form graphForm;
  Form activeForm;
  Form oldForm;
  boolean mouseDown;
  NavigationBar nav;
  TimeSelector ts;
  User selectedUser;
  ArrayList forms;
  int slidingFrames;
  
  // 11/04/2009 Dennis
  public Application()
  {
    forms = new ArrayList();
    mouseDown = false;
    oldForm = null;
    activeForm = null;
    nav = new NavigationBar();
    int formy = nav.height;
    ts = new TimeSelector();
    int formheight = scrH - nav.height - ts.height;
    ts.y = formy + formheight;
    ts.setTimes(db.minTime, db.maxTime);
    
    userForm = new UserForm();
    userForm.y = formy;
    userForm.height = formheight;
    userForm.x = scrW;
    
    artistForm = new ArtistForm();
    artistForm.y = formy;
    artistForm.height = formheight;
    
    albumForm = new AlbumForm();
    albumForm.y = formy;
    albumForm.height = formheight;
    
    graphForm = new GraphForm();
    graphForm.y = formy;
    graphForm.height = formheight;
    
    tracksForm = new TracksForm();
    tracksForm.y = formy;
    tracksForm.height = formheight;
    
    addForm(userForm);
  }
  
  // 11/04/2009 Dennis
  public void draw()
  {
    mouseCursor = 0;
    // animation
    if(activeForm.x != 0)
    {
      if(activeForm.x == scrW || activeForm.x == 0-scrW)
        slidingFrames = 0;
      if(oldForm != null) oldForm.draw();
      if(activeForm.x > 1)
      {
        activeForm.x -= activeForm.x / 5;
        if(oldForm != null) oldForm.x = activeForm.x - scrW;
      }
      else if(activeForm.x < -1)
      {
        activeForm.x -= activeForm.x / 5;
        if(oldForm != null) oldForm.x = activeForm.x + scrW;
      }
      else
      { 
        oldForm = null;
        activeForm.x = 0;
      }
    }
    int mouseState = mousePressed ? 1 : mouseDown ? 2 : 0; // 0: mouse not pressed, 1: mouse down, 2: mouse released
    mouseDown = mousePressed;
    nav.getEvent(mouseX, mouseY, mouseState);
    
    if(ts.getEvent(mouseX, mouseY, mouseState))
    {
      db.qryMin = ts.getMinTime();
      db.qryMax = ts.getMaxTime();
      activeForm.setActive();
    }
    activeForm.updateMouse(mouseX, mouseY, mouseState);
    nav.draw();
    activeForm.draw();
    ts.draw();
    switch(mouseCursor)
    {
      case 0: cursor(ARROW); break;
      case 1: cursor(HAND); break;
      case 2: cursor(MOVE); break;
    }
  }
  
  // 11/04/2009 Dennis
  public void addForm(Form form)
  {
    if(activeForm != null)
    {
      form.x = SCRWIDTH;
      oldForm = activeForm;
      if(activeForm.label != null)
        nav.addLabel(activeForm.label);
    }
    activeForm = form;
    activeForm.setActive();
    nav.title = activeForm.title;
    forms.add(form);
  }
  
  // 11/04/2009 Dennis
  public void removeForm(int n)
  {
    if(n >= forms.size())
      return;
    for(int i = 0; i < n; i++)
      forms.remove(forms.size() - 1);
    oldForm = activeForm;
    activeForm = (Form)forms.get(forms.size() - 1);
    activeForm.x = 0 - scrW;
    nav.title = activeForm.title;
    activeForm.setActive();
  }  
}
